package com.iyundao.io.mapper;

import com.iyundao.io.model.PartyMember;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.InsertProvider;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface PartyMemberMapper {
    @Insert({
        "insert into partymember (id, partymembername, ",
        "partymembersex, partymemberbirthday, ",
        "partymemberjointime, partymemberphone, ",
        "partymemberaddress)",
        "values (#{id,jdbcType=INTEGER}, #{partymembername,jdbcType=VARCHAR}, ",
        "#{partymembersex,jdbcType=VARCHAR}, #{partymemberbirthday,jdbcType=DATE}, ",
        "#{partymemberjointime,jdbcType=DATE}, #{partymemberphone,jdbcType=VARCHAR}, ",
        "#{partymemberaddress,jdbcType=VARCHAR})"
    })
    int insert(PartyMember record);

    @InsertProvider(type= PartyMemberSqlProvider.class, method="insertSelective")
    int insertSelective(PartyMember record);

    @Select("select * from partymember where partymemberphone=#{partymemberphone,jdbcType=VARCHAR}")
    PartyMember selectByPhone(String partymemberphone);

    @Select("select * from partymember where id=#{id,jdbcType=INTEGER}")
    PartyMember selectById(Integer id);

    @Select("select id from partymember order by id desc LIMIT 1")
    int selectLastId();

    @Update("update partymember set partymembername=#{partymembername,jdbcType=VARCHAR}, " +
            "partymembersex=#{partymembersex,jdbcType=VARCHAR}, " +
            "partymemberbirthday=#{partymemberbirthday,jdbcType=DATE}, " +
            "partymemberjointime=#{partymemberjointime,jdbcType=DATE}, " +
            "partymemberphone=#{partymemberphone,jdbcType=VARCHAR}, " +
            "partymemberaddress=#{partymemberaddress,jdbcType=VARCHAR} where id=#{id,jdbcType=INTEGER}")
    int updatePartymember(PartyMember partyMember);

    @Select("select p.partymembername,p.id from partymember p" +
            "inner join activityjoinpeople a" +
            "on p.id = a.partymemberid" +
            "where a.activityid=#(activityid,jdbcType=INTEGER)")
    List<PartyMember> selectAllJoinByActivityId(Integer activityid);

    @Select("select * from partymember where id in " +
            "(select id from partymemberorganization " +
            "where organizationid = #{organizationid,jdbcType=INTEGER})")
    List<PartyMember> selectByOrganizationId(Integer organizationid);

}